package com.hengxinguotong.zhihuichengjian.ui.building;

import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.AualityTestAddPicAdapter;
import com.hengxinguotong.zhihuichengjian.adapter.BuildFileAdapter;
import com.hengxinguotong.zhihuichengjian.adapter.EnterpriseConstructionAdapter;
import com.hengxinguotong.zhihuichengjian.adapter.EnterpriseUserAdapter;
import com.hengxinguotong.zhihuichengjian.bean.BusinessNotify;
import com.hengxinguotong.zhihuichengjian.bean.Construction;
import com.hengxinguotong.zhihuichengjian.bean.Document;
import com.hengxinguotong.zhihuichengjian.bean.FileBean;
import com.hengxinguotong.zhihuichengjian.bean.Notice;
import com.hengxinguotong.zhihuichengjian.bean.User;
import com.hengxinguotong.zhihuichengjian.picturebrowser.view.TaskBigImgActivity;
import com.hengxinguotong.zhihuichengjian.ui.BaseActivity;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @CopyRight 恒信国通信息科技
 * @Author WuLei
 * @Date 2019/1/11
 */

public class NoticeDetailActivity extends BaseActivity implements  AualityTestAddPicAdapter.OnRecyclerViewClickListener, BuildFileAdapter.OnClickListener{
    @BindView(R.id.title_tv)
    HXTextView titleTv;
    @BindView(R.id.title_etv)
    HXTextView titleEtv;
    @BindView(R.id.content_etv)
    HXTextView contentEtv;
    @BindView(R.id.contacts_tv)
    HXTextView contactsTv;
    @BindView(R.id.contact_rv)
    RecyclerView contactRv;
    @BindView(R.id.pic_rv)
    RecyclerView picRv;
    @BindView(R.id.file_rv)
    RecyclerView fileRv;
    @BindView(R.id.no_data_tv)
    HXTextView noDataTv;
    @BindView(R.id.createby_tv)
    HXTextView createbyTv;
    @BindView(R.id.create_time_tv)
    HXTextView createTimeTv;

    private Notice notice;
    private List<String> paths;
    private AualityTestAddPicAdapter aualityTestAddPicAdapter;
    private List<FileBean> fileList;
    private BuildFileAdapter fileAdapter;
    private boolean fromEnterprise = false;
    private List<Construction> noticeConstructions;
    private EnterpriseConstructionAdapter noticeEnterpriseAdapter;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 0:
                    List<BusinessNotify> datas = (List<BusinessNotify>) msg.obj;
                    processNoticers(datas);
                    break;
            }
        }
    };

    private void processNoticers(List<BusinessNotify> datas){
        for (BusinessNotify businessNotify:datas){
            Construction construction = new Construction();
            construction.setConstructionName(businessNotify.getConstructionName());
            List<User> userList = construction.getUserList();
            for (String name:businessNotify.getNames()){
                User user = new User();
                user.setName(name);
                userList.add(user);
            }
            noticeConstructions.add(construction);
        }
        noticeEnterpriseAdapter.setDataList(noticeConstructions);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice_detail);
        ButterKnife.bind(this);
        notice = (Notice) getIntent().getSerializableExtra("notice");
        fromEnterprise = getIntent().getBooleanExtra("fromEnterprise", false);
        initView();
    }

    private void initView() {
        titleTv.setText("通知公告详情");

        titleEtv.setText(notice.getTitle());
        contentEtv.setText(notice.getContent());
        if(fromEnterprise){
            contactsTv.setVisibility(View.GONE);
            noticeConstructions = new ArrayList<>();
            noticeEnterpriseAdapter = new EnterpriseConstructionAdapter(this, noticeConstructions);
            noticeEnterpriseAdapter.setOnChildHeadClickListener(noticeHead);
            contactRv.setAdapter(noticeEnterpriseAdapter);
            contactRv.setLayoutManager(getVerticalLinearLayoutManager());
            List<BusinessNotify> notifies = notice.getContactNameMap();
            Message message = mHandler.obtainMessage();
            message.what = 0;
            message.obj = notifies;
            message.sendToTarget();
        }else {
            contactRv.setVisibility(View.GONE);
            contactsTv.setText(notice.getContactNames());
        }
        if((notice.getPicList() == null || notice.getPicList().size() == 0) && (notice.getFileList() == null || notice.getFileList().size() == 0)){
            noDataTv.setVisibility(View.VISIBLE);
        }else {
            noDataTv.setVisibility(View.GONE);
            LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(this);
            linearLayoutManager1.setOrientation(LinearLayoutManager.HORIZONTAL);
            LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(this);
            linearLayoutManager2.setOrientation(LinearLayoutManager.VERTICAL);
            if(notice.getFileList() != null && notice.getFileList().size() > 0){
                fileRv.setLayoutManager(linearLayoutManager2);
                fileList = notice.getFileList();
                fileAdapter = new BuildFileAdapter(this, fileList);
                fileAdapter.setOnClickListener(this);
                fileRv.setAdapter(fileAdapter);
            }
            if (notice.getPicList() != null && notice.getPicList().size() > 0) {
                picRv.setLayoutManager(linearLayoutManager1);
                paths = notice.getPicList();
                aualityTestAddPicAdapter = new AualityTestAddPicAdapter(this, paths, this, false);
                picRv.setAdapter(aualityTestAddPicAdapter);
            }
        }
        createbyTv.setText(notice.getCreateByName());
        createTimeTv.setText(notice.getCreateTime());
    }

    private  EnterpriseUserAdapter.OnChildHeadClickListener noticeHead = new EnterpriseUserAdapter.OnChildHeadClickListener() {
        @Override
        public void onChildHeadClick(int position, int childPosition) {
            String headPotrait = noticeConstructions.get(position).getUserList().get(childPosition).getHeadPortrait();
            if(!Utils.isEmpty(headPotrait)){
                List<String> picList = new ArrayList<>();
                picList.add(headPotrait);
                TaskBigImgActivity.startActivity(NoticeDetailActivity.this, (ArrayList<String>) picList, position);
            }
        }
    };

    @Override
    public void onItemClick(View v, int position) {
        TaskBigImgActivity.startActivity(NoticeDetailActivity.this, (ArrayList<String>) paths, position);
    }

    @OnClick({R.id.iv_back})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                exit();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        exit();
    }

    private void exit(){
//        setResult(RESULT_OK);
        finish();
    }

    @Override
    public void onClick(Object o) {
        FileBean fileBean = (FileBean) o;
        if(fileBean == null){
            return;
        }
        Document document = new Document();
        document.setName(fileBean.getName());
        document.setPath(fileBean.getPath());
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        String url = document.getPath().replace("type=2", "type=1");
        intent.setData(Uri.parse(url));
        if (intent.resolveActivity(getPackageManager()) != null) {
            final ComponentName componentName = intent.resolveActivity(getPackageManager());
            // 打印Log   ComponentName到底是什么
            Log.e("wl", "componentName = " + componentName.getClassName());
            startActivity(Intent.createChooser(intent, "请选择浏览器"));
        } else {
            showToast("没有匹配的程序");
        }
    }

    private LinearLayoutManager getVerticalLinearLayoutManager() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        return linearLayoutManager;
    }
}
